package com.foreknow.elm.service.impl;

import com.foreknow.elm.dao.CartDao;
import com.foreknow.elm.dao.impl.CartDaoImpl;
import com.foreknow.elm.po.Cart;
import com.foreknow.elm.service.CartService;
import com.foreknow.elm.util.DBUtil;

import java.util.List;

public class CartServiceImpl implements CartService {


    @Override
    public List<Cart> listCart(Cart cart) {
        CartDao cartDao = new CartDaoImpl();
        List<Cart> list=null;
        try {
            list = cartDao.listCart(cart);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public int saveCart(String userId, Integer businessId, Integer foodId) {
        CartDao cartDao = new CartDaoImpl();
        int add=0;
        try {
           add = cartDao.saveCart(userId,businessId,foodId);
            DBUtil.beginTransaction();
            DBUtil.commitTransaction();
        } catch (Exception e) {
            e.printStackTrace();
            try {
                DBUtil.rollbackTransaction();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        } finally {
            DBUtil.close(); // 关闭Connection
        }
        return add;
    }

    @Override
    public Integer updateCart(String userId, Integer businessId, Integer foodId, Integer quantity) {
        Integer update=0;
        CartDao cartDao=new CartDaoImpl();
        try {
            update= cartDao.updateCart(userId, businessId, foodId, quantity);
            DBUtil.beginTransaction();
            DBUtil.commitTransaction();
        } catch (Exception e) {
            e.printStackTrace();
            try {
                DBUtil.rollbackTransaction();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }finally {
            DBUtil.close();
        }
        return update;
    }

    @Override
    public Integer removeCart(Cart cart) {
        Integer del=0;
        CartDao cartDao=new CartDaoImpl();
        try {
            DBUtil.beginTransaction();
            del= cartDao.removeCart(cart);
            DBUtil.commitTransaction();
        } catch (Exception e) {
            e.printStackTrace();
            try {
                DBUtil.rollbackTransaction();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }finally {
            DBUtil.close();
        }
        return del;
    }
}
